<template>
  <el-form style="width:100%;" label-width="100px">
<!--  <el-form :model="upform" :rules="uprules" style="width:100%;" ref="upform" label-width="100px">-->
    <el-form-item label="pdf文档上传分享:">
      <el-button type="primary"
                 @click="upload()"
                 size="mini"
                 class="form-btn"
      >点击上传</el-button>
    </el-form-item>
  </el-form>

<!--  <el-upload-->
<!--    class="upload-demo"-->
<!--    action="#"-->
<!--    accept=".docx,.doc,.pdf"-->
<!--    :before-upload="beforeAvatarUpload"-->
<!--    :on-preview="handlePreview"-->
<!--    :http-request="uploadHttpRequest"-->
<!--    :on-remove="handleRemove"-->
<!--    :file-list="fileList"-->
<!--    list-type="picture">-->
<!--    <el-button size="small" type="primary" @click="submitUpload">点击上传</el-button>-->
<!--    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
<!--  </el-upload>-->
<!--  <el-form-item>-->
<!--    <el-button type="primary" @click="submitImportForm">开始导入</el-button>-->
<!--  </el-form-item>-->
</template>

<script>
/*
export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false
    };
  },
  methods: {
    // 上传文件之前的钩子,上传前对文件的大小和类型进行判断
    uploadData(file) {
      // 打印file，帮助我们了解我们需要的参数
      console.log(file);
      const isExcel = file.name.split('.')[1] === 'docx' || file.name.split('.')[1] === 'doc' || file.name.split('.')[1] === 'pdf';
      const isSize = file.size / 5024 / 5024 < 1;
      if (!isExcel) {
        this.$message({
          message: '只能上传doc或docx文件或pdf!',
          type: 'error',
        });
      }
      if (!isSize) {
        this.$message({
          message: '上传文件大小不能超过 5MB!',
          type: 'error',
        });
      }
      return isExcel && isSize;
    }
  }
}*/
export default {
  data() {
    return {
      fileList: []
      // fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
    };
  },

  methods: {
     upload(){
      const _this = this;
      const fileType = ['doc','docx','pdf']
      const inputFile = document.createElement("input")
      inputFile.type = "file"
      inputFile.style.display = "none"
      document.body.appendChild(inputFile)
      inputFile.click()
      inputFile.addEventListener("change",function() {
        const file = inputFile.files[0];
        var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
        if (!fileType.includes(testmsg)) {
          _this.$message.warning("上传的文件格式只能是'pdf'");
          document.body.removeChild(inputFile);
          return false;
        }
        const formData = new FormData();
        formData.append("file", file);
        _this.$message.success("正在上传中，请稍等...");
        _this.$message.success("只有弹出成功才表示完成上传哦！");
        _this.$http.post('document/upload',
          formData,
          {'Content-type' : 'multipart/form-data',
            emulateJSON: true}
        ).then((response)=>{
            const code = response.data.code;

            if (code !== 200) {
              _this.$message.success("导入失败");

              // alert('申请保存失败');
            }else{
              // alert('保存成功');
              _this.$message.success("导入成功");
              // location.reload();
              this.$router.go(0)
            }
          },
        ).finally(()=>{
          document.body.removeChild(inputFile);
        })
          .catch(function (error) {
          console.log(error);
        });

        // window.request({
        //   url: 'epidemic/static',
        //   data: formData,
        //   success:(res)=> {
        //     if (res.code === 200) {
        //       _this.$message.success(res.message || "导入成功");
        //       // _this.getTableList();
        //       location.reload();
        //     }else {
        //       _this.$message.success("导入失败");
        //     }
        //   },
        //   finally:()=> {
        //     document.body.removeChild(inputFile);
        //   }
        // })
      })
    }

    // submitUpload(file){
    //   console.log(typeof file);
    // },
    // handleRemove(file, fileList) {
    //   console.log(file, fileList);
    // },
    // handlePreview(file) {
    //   console.log(file);
    // }

  },
  mounted() {
    this.upload()
  }
}
</script>
